package ipdesk.model.report.promote.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ibatis.dao.client.DaoManager;
import com.opensymphony.xwork2.ActionSupport;

import ipdesk.common.Codes;
import ipdesk.model.report.consult.bean.ConsultInfoBean;
import ipdesk.model.report.consult.dao.ConsultInfoDao;
import ipdesk.model.report.promote.bean.PromoteInfoBean;


import ecg.dao.DaoService;
import ecg.util.FileService;


public class PromoteInfoDao extends ActionSupport
{
	private static PromoteInfoDao singleton;
	
	protected PromoteInfoDao(){}
	
	public static PromoteInfoDao getInstance()
	{
		if(singleton == null)
		{
			singleton = new PromoteInfoDao();
		}
		
		return singleton;
	}
	
	private DaoManager	manager	= DaoService.getInstance();
	private IPromoteInfo promoteInfo 	= (IPromoteInfo)manager.getDao(IPromoteInfo.class, "ip_desk");
	
	/*
	 * 입력받은 데이터를 DB에 insert 하는 메서드.
	 */
	public int promoteInfoReg(PromoteInfoBean promoteInfoBean) throws Exception
	{
		int lastPromoteSeq = 0;
		
		try{
			// 트랜잭션 시작
			manager.startTransaction();
			
			// 상담정보 insert.
			lastPromoteSeq = promoteInfo.promoteInfoReg(promoteInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}
		
		return lastPromoteSeq;
	}
	/**
	 * 파일데이터 DB에 insert.
	 * @param promoteInfoBean
	 * @throws Exception
	 */
	public void insertFileData(PromoteInfoBean promoteInfoBean) throws Exception
	{
				
		try{
			manager.startTransaction();
			
			promoteInfo.insertFileData(promoteInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}								
	}
	
	public int getDataTotalCount(PromoteInfoBean promoteInfoBean) throws Exception
	{
		int totalDataCount = promoteInfo.getDataTotalCount(promoteInfoBean);
		
		return totalDataCount;
	}
	
	/**
	 * 담당자 AND 센터에 해당하는 데이터를 가지고온다.
	 * 
	 * @param map
	 * @return
	 */
	public List<PromoteInfoBean> getPagingDataList(Map map) throws Exception
	{
		List<PromoteInfoBean> pageDataList = promoteInfo.getPagingDataList(map);
		
		return pageDataList;
	}
	
	/**
	 * 상세 정보 보기
	 */
	
	/**
	 *  해당 promoteSeq에 대한 데이터를 가지고 온다.
	 *
	 */
	public PromoteInfoBean getDetailData(int  promoteSeq)
	{
		PromoteInfoBean promoteInfoBean = promoteInfo.getDataOneRow( promoteSeq, "promote.promoteDetailData");
		
		// 시간 잘라내기
		String modTime = promoteInfoBean.getModifyTime();
		if(modTime != null && (!modTime.equals("")))
		{
			String modifyTime = promoteInfoBean.getModifyTime().substring(0, 10);
			
			// 변환된 스트링을 다시 빈에 저장.
			promoteInfoBean.setModifyTime(modifyTime);
		}
		
		String promoteContent = promoteInfoBean.getPromoteContent().replace("\n", "<br/>");
		promoteInfoBean.setPromoteContent(promoteContent);
		
		return promoteInfoBean;
	}
	
	
	/**
	 *  해당 promoteSeq에 대한 데이터를 가지고 온다.
	 *
	 */
	public PromoteInfoBean getModifyData(int promoteSeq)
	{
		PromoteInfoBean promoteInfoBean = promoteInfo.getDataOneRow(promoteSeq, "promote.promoteDetailData");
		
				
		return promoteInfoBean;
	}
	
	
	/**
	 * 파일 삭제
	 */
	public boolean fileDelete(int promoteSeq)
	{
		boolean deleteFlag = false;
		
		// promoteSeql에 해당하는 파일 데이터를 가지고 온다.
		PromoteInfoBean promoteInfoBean = promoteInfo.getDataOneRow(promoteSeq, "promote.promoteUploadData");
		
		// 데이터가 존재한다면 데이터 삭제. 파일 삭제
		if(promoteInfoBean != null)
		{
			try{
				manager.startTransaction();
				// 파일 데이터 삭제
				promoteInfo.deleteDataOneRow(promoteSeq, "promote.deleteUploadData");
				manager.commitTransaction();
				
				String filePathPrefix = getText("path.upload.prefix");
				String filePath = promoteInfoBean.getPathName() + promoteInfoBean.getUploadFileName();
				// 파일 삭제
				FileService fs = new FileService();
				fs.deleteFile(filePathPrefix + filePath);
				
				deleteFlag = true;
			}finally{
				manager.endTransaction();
			}
		}
		
		return deleteFlag;
	}
	
	/**
	 *  해당 promoteSeq에 대한 파일데이터를 삭제한다.
	 *
	 */
	public void deleteDataOneRow(int promoteSeq)
	{
		promoteInfo.deleteDataOneRow(promoteSeq, "promote.deleteUploadData");
	}
	
	/**
	 *  해당 promoteSeq에 대한 파일데이터를 가지고 온다.
	 *
	 */
	public PromoteInfoBean getUploadData(int promoteSeq)
	{
		PromoteInfoBean promoteInfoBean = promoteInfo.getDataOneRow(promoteSeq, "promote.promoteUploadData");
				
		return promoteInfoBean;
	}
	
	/**
	 *  해당 promoteSeq에 대한 데이터를 업데이트 한다.
	 */
	public int promoteInfoUpdate(PromoteInfoBean promoteInfoBean) throws Exception
	{
		
		int result = 0;
		
		try{
			manager.startTransaction();
			
			// 지재권 상담일지 데이터 업데이트.
			result = promoteInfo.promoteInfoUpdate(promoteInfoBean, "promote.updatePromoteData");
			
			if(result != 0 && promoteInfoBean.getUpload() != null)
			{
				this.insertFileData(promoteInfoBean);
			}
			manager.commitTransaction();
			
		}finally{
			manager.endTransaction();
		}
		
		
		return result;
	}
	
	
	/**
	 * 데이터 삭제
	 * 
	 */
	public boolean promoteInfoDelete(int promoteSeq)
	{
		
		boolean fileDeleteFlag = false; 
		boolean deleteFlag = false;
		
		try{
			manager.startTransaction();
			// 파일 테이블 로우 및 파일 데이터 삭제.
			fileDeleteFlag = this.fileDelete(promoteSeq);
			
			// consult 데이터 삭제.
			promoteInfo.deleteDataOneRow(promoteSeq, "promote.deletePromoteData");
				
			deleteFlag = true;
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}
		
		return deleteFlag;
	}
}
